package com.peanut.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.peanut.persistence.ResourceMapper;
import com.peanut.persistence.RoleMapper;
import com.peanut.persistence.UserMapper;
import com.peanut.model.Resource;
import com.peanut.model.Role;
import com.peanut.model.User;

@Service
public class InitializationService {

	private ResourceMapper resourceDao;
	
	private RoleMapper roleDao;
	
	private UserMapper userDao;

	@Autowired
	public void setResourceMapper(ResourceMapper resourceDao) {
		this.resourceDao = resourceDao;
	}

	@Autowired
	public void setRoleMapper(RoleMapper roleDao) {
		this.roleDao = roleDao;
	}

	@Autowired
	public void setUserMapper(UserMapper userDao) {
		this.userDao = userDao;
	}
	
	public void initApplication() {
		initRecource();
		initRole();
		initUser();
	}

	private void initUser() {
//		User user1 = new User();
//		user1.setUserCode("admin");
//		user1.setPassword("1");
//		user1.setEnabled(BooleanUtils.toInteger(true));
//		user1.setAccountNonExpired(BooleanUtils.toInteger(true));
//		user1.setAccountNonLocked(BooleanUtils.toInteger(true));
//		user1.setCredentialsNonExpired(BooleanUtils.toInteger(true));
//		List<Role> roles = new ArrayList<Role>();
//		roles.add(roleDao.get(1));
//		user1.setRoles(roles);
//		userDao.save(user1);
	}

	private void initRole() {
		Role role1 = new Role();
		role1.setName("ROLE_ADMIN");
		role1.setDescription("管理员");
		role1.setEnabled(1);
		Set<Resource> modules = new HashSet<Resource>();
//		modules.add(resourceDao.get(1));
//		modules.add(resourceDao.get(2));
//		modules.add(resourceDao.get(3));
//		modules.add(resourceDao.get(4));
//		modules.add(resourceDao.get(5));
//		modules.add(resourceDao.get(6));
		role1.setResources(modules);
		
		Role role2 = new Role();
		role2.setName("ROLE_USER");
		role2.setDescription("普通用户");
		role2.setEnabled(1);
		
//		roleDao.save(role1);
//		roleDao.save(role2);
	}

	private void initRecource() {
		Resource resource1 = new Resource();
		resource1.setName("系统管理");
		resource1.setParentId(0);
		resource1.setAction("#");
		resource1.setIsDisplay(1);
		resource1.setType("COMMON");
		resource1.setEnabled(1);
//		resource1 = resourceDao.save(resource1);
		
		Resource resource2 = new Resource();
		resource2.setName("模块管理");
		resource2.setParentId(resource1.getId());
		resource2.setAction("#");
		resource2.setType("COMMON");
		resource2.setIsDisplay(1);
		resource2.setOpenView("peanut.module.Manager");
		resource2.setEnabled(1);
		resource2.setOrderNumber(1);
//		resourceDao.save(resource2);
		
		Resource resource3 = new Resource();
		resource3.setName("角色管理");
		resource3.setParentId(resource1.getId());
		resource3.setType("COMMON");
		resource3.setIsDisplay(1);
		resource3.setAction("#");
		resource3.setEnabled(1);
		resource2.setOrderNumber(2);
//		resourceDao.save(resource3);
		
		Resource resource4 = new Resource();
		resource4.setName("用户管理");
		resource4.setParentId(resource1.getId());
		resource4.setType("COMMON");
		resource4.setIsDisplay(1);
		resource4.setAction("#");
		resource4.setEnabled(1);
		resource2.setOrderNumber(3);
//		resourceDao.save(resource4);
		
		Resource resource5 = new Resource();
		resource5.setName("基本信息");
		resource5.setParentId(0);
		resource5.setType("COMMON");
		resource5.setIsDisplay(1);
		resource5.setAction("#");
		resource5.setEnabled(1);
//		resource5 = resourceDao.save(resource5);
		
		Resource resource6 = new Resource();
		resource6.setName("信息管理");
		resource6.setParentId(resource5.getId());
		resource6.setType("COMMON");
		resource6.setIsDisplay(1);
		resource6.setAction("#");
		resource6.setEnabled(1);
//		resourceDao.save(resource6);
	}
	
}
